package tree.easy;

import tree.TreeNode;

/**
 * 二叉树的坡度
 */
public class LeetCode563 {

    int title;

    public int findTilt(TreeNode root) {
        sum(root);
        return title;
    }

    public int sum(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int sumLeft = sum(node.left);
        int sumRight = sum(node.right);
        title += Math.abs(sumLeft - sumRight);
        return sumLeft + sumRight + node.val;
    }

}
